import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout/home'
  },
  {
    path: '/login',
    component: () => import('@/views/Login')
    /* 路由独享守卫 */
    /*  beforeEnter: (to, from, next) => {
      if (to.path === '/login' && JSON.parse(localStorage.getItem('geek-itheima'))) {
        next(false)
      } else {
        next()
      }
    } */
  },
  {
    path: '/layout',
    component: () => import('@/views/Layout'),
    children: [
      {
        path: 'home',
        component: () => import('@/views/Home')
      },
      {
        path: 'user',
        component: () => import('@/views/User')
      }
    ]
  },
  {
    path: '/search',
    component: () => import('@/views/Search')
  },
  {
    path: '/search/:keywords',
    component: () => import('@/views/Search/SearchResult')
  },
  {
    path: '/article_detail',
    component: () => import('@/views/ArticleDetail')
  },
  {
    path: '/user_editor',
    component: () => import('@/views/User/UserEdit')
  },
  {
    path: '/chat',
    component: () => import('@/views/Chat')
  }
]

const router = new VueRouter({
  routes
})
/* 全局前置路由守卫 */
router.beforeEach((to, from, next) => {
  if (to.path === '/login' && localStorage.getItem('geek-itheima').length > 0) {
    next(false)
  } else {
    next()
  }
})
export default router
